package dao;

import java.io.Serializable;
import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;


import Hibernate.HibernateUtil;;

public abstract class AbstractDAO<P,K extends Serializable> {
	protected abstract  Class getPOJOClass();
	 public  P get(K key) throws HibernateException {
		P p = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		try {
			p=(P)session.get(getPOJOClass(), key);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace(); 
			
		}finally{
			session.close();
		}
		return p;
	}
	public ArrayList<P> getList() throws HibernateException{
		ArrayList<P> listP= new ArrayList<P>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = String.format("select obj from %s obj",getPOJOClass().getName());
		Query query = session.createQuery(hql);
		listP = (ArrayList<P>) query.list();
		session.close();
		return listP;
	}

}
